import 'package:flutter/material.dart';

/**
 * 商品
 */
class Product {

  final String name;
  const Product({
    this.name
  });
}

typedef void CartChangedCallback(Product product,bool inCart);

class ShoppingListItem extends StatelessWidget {

  final Product product;
  final bool inCart;
  final CartChangedCallback cartChangedCallback;

  ShoppingListItem({
        this.product,
        this.inCart,
        this.cartChangedCallback
      }):super(key: new ObjectKey(product));

  Color _getColor(BuildContext context) {
    return inCart ? Theme.of(context).primaryColor : Colors.black54;
  }

  TextStyle _getTextStyle(BuildContext context) {
    if (inCart) return null;

    return new TextStyle(
      color: Colors.black54,
      decoration: TextDecoration.lineThrough
    );
  }

  @override
  Widget build(BuildContext context) {
    return new ListTile(
      onTap: () {
        cartChangedCallback(product,!inCart);
      },
      leading: new CircleAvatar(
        backgroundColor: _getColor(context),
        child: new Text(product.name[0]),
      ),
      title: new Text(product.name,style: _getTextStyle(context),),
    );
  }

}